package com.dh.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dh.common.ResultMapUtil;
import com.dh.pojo.Saleinfo;
import com.dh.service.SaleinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 销售记录相关的controller
 */
@Controller
@RequestMapping(value = "/saleinfo")
public class SaleinfoController {

    @Autowired
    private SaleinfoService saleinfoService;

    /**
     * 转向销售记录页面
     */
    @RequestMapping
    public String saleinfo(){
        return "/saleinfo";
    }

    /**
     * 分页查询销售记录列表
     * @param param 查询参数
     * @param pageNum 起始页数
     * @param pageSize 每页多少条数据
     * @return
     */
    @RequestMapping(value = "/saleinfoQueryPage")
    @ResponseBody
    public Object saleinfoQueryPage(String param, @RequestParam(defaultValue = "1")int pageNum,@RequestParam(defaultValue = "10")int pageSize){
        try{
            //查询对应的数据
            IPage<Saleinfo> iPage = saleinfoService.selectSaleinfoPage(pageNum,pageSize,param);
            //获取mysql的分页查询结果
            return ResultMapUtil.getHashMapMysqlPage(iPage);
        } catch (Exception e){
            //如果出错，把异常信息返回给前端
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 转向销售记录新增页面
     */
    @RequestMapping(value = "/saleinfoPage")
    public String saleinfoPage(){
        return "/saleinfoPage";
    }

    /**
     * 添加一个销售记录
     */
    @RequestMapping(value = "/saleinfoAdd")
    @ResponseBody
    public Object saleinfoAdd(Saleinfo saleinfo){
        try{
            //获取创建时间
            int i = saleinfoService.addSaleinfo(saleinfo);
            //新增一条出入库信息
            return ResultMapUtil.getHashMapSave(i);
            //保存,并统一格式返回
        } catch (Exception e){
            //如果出错，把异常信息返回给前端
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 转向销售记录编辑页面
     */
    @RequestMapping(value = "/saleinfoQueryById")
    public String saleinfoQueryById(@RequestParam(name = "id",required = true)Integer id, Model model){
        //根据id查询药品
        Saleinfo saleinfo = saleinfoService.querySaleinfoById(id);
        //model调用逻辑处理
        model.addAttribute("obj",saleinfo);
        //跳转页saleinfoPage.html
        return "/saleinfoPage";
    }

    /**
     * 修改一个销售记录
     */
    @RequestMapping(value = "/saleinfoEdit")
    @ResponseBody
    public Object saleinfoEdit(Saleinfo saleinfo){
        try{
            //修改一条药品出入库信息
            int i = saleinfoService.editSaleinfo(saleinfo);
            //保存,并统一格式返回
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e){
            //如果出错，把异常信息返回给前端
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 删除一个销售记录
     * @param id 根据id删除
     * @return 返回删除的数据
     */
    @RequestMapping(value = "/saleinfoDelById")
    @ResponseBody
    public Object saleinfoDelById(Integer id){
        try{
            //根据id删除数据
            int i = saleinfoService.delSaleinfoById(id);
            //保存,并统一格式返回
            return ResultMapUtil.getHashMapDel(i);
        } catch (Exception e){
            //如果出错，把异常信息返回给前端
            return ResultMapUtil.getHashMapException(e);
        }
    }
}